<template>
  <div class="editChannel">
    <!-- 我的频道-->
    <van-cell class="my-cell" :border="false">
      <span slot="title" class="left-title">我的频道</span>
      <van-button @click="isEdit=!isEdit" size="mini" type="default" plain round color="green">{{isEdit ? '完成' : '编辑'}}
      </van-button>
    </van-cell>
    <!-- 我的频道列表 -->
    <van-grid class="list-grid" :gutter="10">
      <van-grid-item class="channel-item" @click="myChannelClick(ind,item)" :icon="(isEdit && ind !==0) ? 'close': ''"
        v-for="(item,ind) in myChannels" :key="ind">
        <span class="text" :class="{active: active === ind}" slot="text">{{ item.name }}</span>
      </van-grid-item>
    </van-grid>

    <!-- 推荐频道 -->
    <van-cell value="内容" class="recommend-cell" :border="false">
      <span class="left-title">频道推荐</span>
    </van-cell>
    <van-grid class="recommend-grid list-grid" :gutter="10" direction="horizontal">
      <van-grid-item @click="recommmondClick(item)" v-for="item in recommendChannels" :key="item.id" icon="plus"
        :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannels } from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
const HMTT_CHANNEL = 'hmtt_channel'
export default {
  name: 'channel-edit',
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  methods: {
    // 加载频道列表
    async loadChannels () {
      try {
        const { data: res } = await getAllChannels()
        this.allChannels = res.data.channels
        console.log(this.allChannels)
      } catch (err) {
        this.$toast('获取频道列表失败')
      }
    },
    // 点击推荐频道
    async recommmondClick (item) {
      // 父向子传值，传的是引用类型，子可以修改但不能直接给该值赋值
      this.myChannels.push(item)
      if (this.user) {
        // 已登录
        // console.log('存储到数据库')
        try {
          await addUserChannels({
            id: item.id,
            seq: this.myChannels.length
          })
          this.$toast.success('添加成功')
        } catch (err) {
          this.$toast.fail('添加失败')
        }
      } else {
        // 未登录
        console.log('存储到本地')
        setItem(HMTT_CHANNEL, this.myChannels)
      }
    },
    // 点击我的频道
    myChannelClick (index, item) {
      // 判断是否处于编辑状态
      if (this.isEdit) {
        // 判断是否为推荐
        if (index === 0) return
        // 执行删除操作
        if (index <= this.active) {
          this.$emit('sendData', {
            close: true,
            index: this.active - 1
          })
        }
        this.myChannels.splice(index, 1)
        // 删除数据持久化
        this.deleteChannelsStorage()
      } else {
        console.log('跳转')
        this.$emit('sendData', {
          close: false,
          index: index
        })
      }
    },
    // 删除数据持久化
    deleteChannelsStorage () {
      try {
        if (this.user) {
          // 数据存到数据库
        } else {
          // 数据存到本地
          setItem(HMTT_CHANNEL, this.channels)
        }
      } catch (err) {
        this.$toast('删除频道失败')
      }
    }
  },
  computed: {
    // 通过计算的得到推荐频道的数据
    recommendChannels () {
      return this.allChannels.filter(item => {
        return !this.myChannels.find(channelItem => item.id === channelItem.id // 判断find条件
        )
      })
    },
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    this.loadChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.editChannel {
  padding-top: 70px;
  .my-cell {
    align-items: center;
    &::after {
      border: none;
    }
  }
  .left-title {
    font-size: 32px;
    color: #333;
  }
  .van-button--default {
    padding: 5px 20px;
  }
  //公共样式
  /deep/ .list-grid {
    .van-grid-item__icon + .van-grid-item__text {
      margin-top: 0px;
    }
    .van-grid-item {
      height: 86px;
      .text {
        font-size: 28px;
      }
      .van-grid-item__content {
        background-color: #f4f5f6;
        border-radius: 6px;
        &::after {
          border: none;
        }
      }
      .van-icon-close {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 36px;
        color: #ccc;
      }
      .active {
        color: red;
      }
    }
  }
  //推荐的cell
  .recommend-cell {
    .left-title {
      font-size: 32px;
      color: #333;
    }
  }
  //推荐的宫格样式
  /deep/ .recommend-grid {
    .van-grid-item__icon {
      font-size: 24px;
    }
    .van-grid-item__text {
      font-size: 28px;
      margin-top: 0;
    }
    .van-icon-plus {
      color: black;
    }
  }
}
</style>
